fibers
This is a library to execute a number of lightweight asynchronous tasks (a.k.a, fibers).
Note that fibers
heavily uses futures to
represent asynchronous task. If you are not familiar with it,
we recommend that you refer the README.md
and TUTORIAL.md
of futures
before reading the following.
This library also uses mio to achieve efficient asynchronous I/O handling (mainly for networking primitives). However, its existence is hidden from the user, so you do not usually have to worry about it.
Future
is an excellent way to represent asynchronous task.
It is intuitive, easily composed with other futures to represent a complicated task,
without runtime overhead.
But, there is a remaining problem that
"How to efficiently execute (possibility a very large amount of) concurrent tasks?".
fibers
is an answer to the problem.
Conceptually, the responsibility of fibers
is very simple.
It represents an asynchronous task (a.k.a., fiber) as a future instance.
And there is an executor that takes futures and executes them like following.
// Creates an executor.
let mut executor = new.unwrap;
// Spawns fibers (i.e., passes futures to the executor).
executor.spawn;
executor.spawn;
// Executes them.
executor.run.unwrap;
Fibers may be run on different background threads, but the user does not need to notice it. If it runs on machines with a large number of processors, performance will improve naturally.
Roughly speaking, if a future returns Async::NotReady
response to a call of Future::poll
method,
the fiber associated with the future will move into the "waiting" state.
Then, it is suspended (descheduled) until any event in which the future is interested happens
(e.g., waits until data is arrived on a target TCP socket).
Finally, if a future returns Async::Ready
response, the fiber will be regarded as completed and
the executor will drop the fiber.
This library provides primitives for writing programs in an efficient asynchronous fashion (See documentations of net, sync, io, time modules for more details).
The main concern of this library is "how to execute fibers". So it is preferred to use external crates (e.g., handy_async) to describe "how to represent asynchronous tasks".
Installation
First, add following lines to your Cargo.toml
:
[]
= "0.1"
= "0.1" # In practical, `futures` is mandatory to use `fibers`.
Next, add this to your crate:
extern crate fibers;
extern crate futures;
Several runnable examples are given in the next section.
Examples
The following are examples of writing code to perform asynchronous tasks.
Other examples are found in "fibers/examples" directory. And you can run an example by executing the following command.
Calculation of fibonacci numbers
// See also: "fibers/examples/fibonacci.rs"
extern crate fibers;
extern crate futures;
use ;
use Future;
TCP Echo Server and Client
An example of TCP echo server listening at the address "127.0.0.1:3000":
// See also: "fibers/examples/tcp_echo_srv.rs"
extern crate fibers;
extern crate futures;
extern crate handy_async;
use io;
use ;
use TcpListener;
use ;
use ;
use AllowPartial;
And the code of the client side:
// See also: "fibers/examples/tcp_echo_cli.rs"
extern crate fibers;
extern crate futures;
extern crate handy_async;
use ;
use TcpStream;
use ;
use ;
use AllowPartial;
Real examples using fibers
Here is a list of known projects using fibers
:
- erl_dist: Erlang Distribution Protocol Implementation
- miasht: Minimum Asynchronous HTTP server/client
- rustun: STUN(RFC5389) server/client Implementation
- rusturn: TURN(RFC5766) server/client Implementation
License
This library is released under the MIT License.
See the LICENSE file for full license information.
Copyright (c) 2016 DWANGO Co., Ltd. All Rights Reserved.